package com.mrh.mall.admin.controller;

import com.mrh.mall.admin.dto.PmsBrandParam;
import com.mrh.mall.admin.service.PmsBrandService;
import com.mrh.mall.common.api.CommonPage;
import com.mrh.mall.common.api.CommonResult;
import com.mrh.mall.mbg.model.PmsBrand;
import com.mrh.mall.mbg.model.PmsBrandExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

/**
 * 实现pmsbrand表的添加、修改、删除及分页查询接口
 * 品牌管理
 */
@RestController
@RequestMapping("/brand")
@Api(tags = "PmsBrandController",description = "商品品牌管理")
public class PmsBrandController {
    @Autowired
    PmsBrandService pmsBrandService;

    @RequestMapping(value = "/listAll",method = RequestMethod.GET)
    @ApiOperation(value = "获取全部品牌列表")
    public CommonResult<List<PmsBrand>> getBrandList(){
        return CommonResult.success(pmsBrandService.listAllBrand());
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ApiOperation(value = "添加品牌")
    public CommonResult create(@Validated @RequestBody PmsBrandParam pmsBrand){
        CommonResult commonResult;
        int brand = pmsBrandService.createBrand(pmsBrand);
        if (brand==1){
            commonResult = CommonResult.success(brand);
        }else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }

    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "更新品牌")
    public CommonResult update(@PathVariable("id") Long id,
                               @Validated @RequestBody PmsBrandParam pmsBrandParam){
        CommonResult commonResult;
        int brand = pmsBrandService.updateBrand(id,pmsBrandParam);
        if (brand==1){
            commonResult = CommonResult.success(brand);
        }else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }

    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "删除品牌")
    public CommonResult delete(@PathVariable("id") Long id){
        CommonResult commonResult;
        int brand = pmsBrandService.deleteBrand(id);
        if (brand==1){
            commonResult = CommonResult.success(brand);
        }else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value = "根据品牌名称分页获取品牌列表")
    public CommonResult<CommonPage<PmsBrand>> getList(@RequestParam(value = "keyword",required = false) String keyword,
                                                      @RequestParam(value = "showStatus",required = false) Integer showStatus,
                                                      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<PmsBrand> pmsBrands = pmsBrandService.listBrand(keyword, showStatus, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(pmsBrands));
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据编号查询品牌信息")
    public CommonResult<PmsBrand> getItem(@PathVariable("id") Long id){
        return CommonResult.success(pmsBrandService.getItem(id));
    }

    @RequestMapping(value = "/delete/batch",method = RequestMethod.POST)
    @ApiOperation(value = "批量删除品牌")
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids){
        int count = pmsBrandService.deleteBrands(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @RequestMapping(value = "/update/showStatus",method = RequestMethod.POST)
    @ApiOperation(value = "批量更新显示状态")
    public CommonResult updateShowStatus(@RequestParam("ids") List<Long> ids,
                                         @RequestParam("showStatus") Integer showStatus){
        int count = pmsBrandService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @RequestMapping(value = "/update/factoryStatus",method = RequestMethod.POST)
    @ApiOperation(value = "批量更新厂家制造商状态")
    public CommonResult updateFactoryStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("factoryStatus") Integer factoryStatus){
        int count = pmsBrandService.updateFactoryStatus(ids, factoryStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
